package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.*;

import java.text.SimpleDateFormat;
import java.util.*;


/** @author wangfupeng */
public class MybatisPluginComment extends PluginAdapter {


    private FullyQualifiedJavaType apiModel;
    private FullyQualifiedJavaType apiModelProperty;
    private boolean addGWTInterface;
    private boolean suppressJavaInterface;
    private FullyQualifiedJavaType data;
    private FullyQualifiedJavaType noArgsConstructor;
    private FullyQualifiedJavaType builder;
    private FullyQualifiedJavaType allArgsConstructor;

    public MybatisPluginComment() {
        super();
        apiModel = new FullyQualifiedJavaType("io.swagger.annotations.ApiModel");
        apiModelProperty = new FullyQualifiedJavaType("io.swagger.annotations.ApiModelProperty");
        allArgsConstructor = new FullyQualifiedJavaType("lombok.AllArgsConstructor");
        builder = new FullyQualifiedJavaType("lombok.Builder");
        data = new FullyQualifiedJavaType("lombok.Data");
        noArgsConstructor = new FullyQualifiedJavaType("lombok.NoArgsConstructor");
    }


    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }


    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        topLevelClass.getJavaDocLines().clear();
        topLevelClass.addImportedType(apiModel);
        topLevelClass.addImportedType(apiModelProperty);
        topLevelClass.addImportedType(data);
        topLevelClass.addImportedType(builder);
        topLevelClass.addImportedType(allArgsConstructor);
        topLevelClass.addImportedType(noArgsConstructor);
        topLevelClass.addAnnotation("@ApiModel");
        topLevelClass.addAnnotation("@Data");
        topLevelClass.addAnnotation("@Builder");
        topLevelClass.addAnnotation("@NoArgsConstructor");
        topLevelClass.addAnnotation("@AllArgsConstructor");
        return true;
    }


    @Override
    public boolean modelFieldGenerated(Field field,
                                       TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        field.getJavaDocLines().clear();
        field.addAnnotation("@ApiModelProperty(\""+introspectedColumn.getRemarks().replaceAll("\"","“")+"\")");
        return true;
    }


    @Override
    public boolean modelGetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }


    @Override
    public boolean modelSetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }

//    private void comment(JavaElement element, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
//
//        element.getJavaDocLines().clear();
//        element.addJavaDocLine("/**");
//        String remark = introspectedColumn.getRemarks();
//        if (remark != null && remark.length() > 1) {
//            element.addJavaDocLine(" * " + remark);
//            element.addJavaDocLine(" *");
//        }
//        element.addJavaDocLine(" * Table:     " + introspectedTable.getFullyQualifiedTable());
//        element.addJavaDocLine(" * Column:    " + introspectedColumn.getActualColumnName());
//        element.addJavaDocLine(" * Nullable:  " + introspectedColumn.isNullable());
//        element.addJavaDocLine(" */");
//    }

}
